package com.lhb.service.Impl;

import com.lhb.mapper.MainMapper;
import com.lhb.pojo.entity.Award;
import com.lhb.pojo.entity.Course;
import com.lhb.pojo.entity.Family;
import com.lhb.pojo.entity.Student;
import com.lhb.pojo.vo.AwardVo;
import com.lhb.pojo.vo.CourseVo;
import com.lhb.pojo.vo.FamilyVo;
import com.lhb.service.MainJFrameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Service
@Slf4j
public class MainJFrameServiceImpl implements MainJFrameService {
    @Autowired
    private MainMapper mainMapper;


    /**
     * 动态查询学生信息
     *
     * @param student
     * @return
     */

    public List<Student> getStudents(Student student) {

        log.info("动态查询student表得到学生信息");
     List<Student> students = mainMapper.getStudents(student);

        return students;
    }


    /**
     * 动态获得family表的数据
     *
     * @param students
     * @return
     */
    @Override
    public List<FamilyVo> getFamilies(List<Student> students) {


        log.info("根据条件:{}动态获得family表的信息",students);

        List<FamilyVo> families = new ArrayList<>();
        for (Student student : students) {

            List<Family> familyList = mainMapper.getFamily(student);

            for (Family family : familyList) {

                FamilyVo familyVo = new FamilyVo();
                BeanUtils.copyProperties(student,familyVo );
                BeanUtils.copyProperties(family,familyVo);
                families.add(familyVo);
            }





        }


        return families;
    }

    /**
     *报存数据
     * @param dataVector
     */
    public void saveStudent(Vector<Vector> dataVector) {
        log.info("删除student表所有内容");

        mainMapper.deleteStudent();

        log.info("将JTabel内容:{}重写到student表中", dataVector);

        List<Student> students = new Vector<>();
        for (Vector vector : dataVector) {

            Student student = new Student();
            student.setGrade(vector.get(0)==null?"":vector.get(0).toString());
            student.setProfession(vector.get(1)==null?"":vector.get(1).toString());
            student.setMyClass(vector.get(2)==null?"":vector.get(2).toString());
            student.setStudentId(vector.get(3)==null?"":vector.get(3).toString());
            student.setStudentName(vector.get(4)==null?"":vector.get(4).toString());
            student.setSex(vector.get(5)==null?"":vector.get(5).toString());
            student.setAge( vector.get(6)==null?"":vector.get(6).toString());
            student.setBirthday( vector.get(7)==null?"":vector.get(7).toString());
            student.setMyNative(vector.get(8)==null?"":vector.get(8).toString());
            student.setAddress(vector.get(9)==null?"":vector.get(9).toString());
            student.setPrivilegeStatus( vector.get(10)==null?"":vector.get(10).toString());
            student.setPhone(vector.get(11)==null?"":vector.get(11).toString());

            students.add(student);
        }



        mainMapper.addStudent(students);

    }

    /**
     * 保存数据
     * @param dataVector
     */
    public void saveFamily(Vector<Vector> dataVector) {

        log.info("删除Family表所有内容");

        mainMapper.deleteFamily();

        log.info("将JTable中的数据重写到family表中");


        List<Family> families = new ArrayList<>();

        for (Vector vector : dataVector) {

            Family family = new Family();
            family.setStudentId(vector.get(3)==null?"":vector.get(3).toString());
            family.setTitle(vector.get(6)==null?"":vector.get(6).toString());
            family.setFamilyName(vector.get(7)==null?"":vector.get(7).toString());
            family.setWorkPlace( vector.get(8)==null?"":vector.get(8).toString());
            family.setBusiness( vector.get(9)==null?"":vector.get(9).toString());
            family.setPhone(vector.get(10)==null?"":vector.get(10).toString());


            families.add(family);
        }

        mainMapper.addFamily(families);

    }

    /**
     * 动态获得award表的数据
     * @param students
     * @return
     */
    public List<AwardVo> getAward(List<Student> students) {

        log.info("根据条件:{}动态获得award表的信息",students);

        List<AwardVo> awardVoList = new ArrayList<>();
        for (Student student : students) {

            List<Award> awardList = mainMapper.getAward(student);

            for (Award award : awardList) {

                AwardVo awardVo = new AwardVo();
                BeanUtils.copyProperties(student, awardVo);
                BeanUtils.copyProperties(award, awardVo);
                awardVoList.add(awardVo);
            }





        }


        return awardVoList;
    }

    /**
     * 保存数据
     * @param dataVector
     */
    public void saveAward(Vector<Vector> dataVector) {


        log.info("删除award表所有内容");

        mainMapper.deleteAward();

        log.info("将JTable中的数据重写到award表中");


        List<Award> awards = new ArrayList<>();

        for (Vector vector : dataVector) {

            Award award = new Award();
            award.setStudentId(vector.get(3)==null?"":vector.get(3).toString());
            award.setPrizeName(vector.get(6)==null?"":vector.get(6).toString());
            award.setPrize(vector.get(7)==null?"":vector.get(7).toString());
            award.setAwardTime( vector.get(8)==null?"":vector.get(8).toString());



            awards.add(award);
        }

        mainMapper.addAward(awards);


    }

    /**
     * 动态获得course表的数据
     *
     * @param students
     * @return
     */
    public List<CourseVo> getCourse(List<Student> students) {

        log.info("根据条件:{}动态获得award表的信息",students);

        List<CourseVo> courseVoList = new ArrayList<>();
        for (Student student : students) {

            List<Course> courseList = mainMapper.getCourse(student);

            for (Course course : courseList) {

                CourseVo courseVo = new CourseVo();
                BeanUtils.copyProperties(student, courseVo);
                BeanUtils.copyProperties(course, courseVo);
                courseVoList.add(courseVo);
            }





        }



        return courseVoList;
    }

    @Override
    public void saveCourse(Vector<Vector> dataVector) {


        log.info("删除course表所有内容");

        mainMapper.deleteCourse();

        log.info("将JTable中的数据重写到course表中");


        List<Course> courseList = new ArrayList<>();

        for (Vector vector : dataVector) {

            Course course = new Course();
            course.setStudentId(vector.get(3)==null?"":vector.get(3).toString());
            course.setCourse(vector.get(6)==null?"":vector.get(6).toString());
            course.setScore(vector.get(7)==null?"":vector.get(7).toString());



            courseList.add(course);
        }

        mainMapper.addCourse(courseList);




    }
}
